package cn.zyb.pet.controller;

import cn.zyb.pet.service.IPetDetailService;
import cn.zyb.pet.domain.PetDetail;
import cn.zyb.pet.query.PetDetailQuery;
import cn.zyb.basic.util.AjaxResult;
import cn.zyb.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/petDetail")
public class PetDetailController {

  @Autowired
  public IPetDetailService petDetailService;

  /**
   * 接口：添加或修改
   * @param petDetail  传递的实体
   * @return AjaxResult 响应给前端
   */
  @PutMapping
  public AjaxResult addOrUpdate(@RequestBody PetDetail petDetail){
    try {
      if( petDetail.getId()!=null)
        petDetailService.update(petDetail);
      else
        petDetailService.add(petDetail);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"操作失败");
    }
  }

  /**
   * 接口：删除
   * @param id
   * @return AjaxResult 响应给前端
   */
  @DeleteMapping(value="/{id}")
  public AjaxResult del(@PathVariable("id") Long id){
    try {
      petDetailService.del(id);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"删除失败");
    }
  }

  /**
   * 接口：批量删除
   * @param ids
   * @return AjaxResult 响应给前端
   */
  @PatchMapping
  public AjaxResult patchDel(@RequestBody List<Long> ids){
    try {
      petDetailService.patchDel(ids);
      return new AjaxResult();
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false,"批量删除失败");
    }
  }

  /**
   * 接口：查询单个对象
   * @param id
   */
  @GetMapping("/{id}")
  public PetDetail findOne(@PathVariable("id")Long id) {
    return petDetailService.findOne(id);
  }


  /**
   * 接口：查询所有
   * @return
   */
  @GetMapping
  public List<PetDetail> findAll(){
    return petDetailService.findAll();
  }


  /**
   * 接口：分页查询或高级查询
   * @param query 查询对象
   * @return PageList 分页对象
   */
  @PostMapping
  public PageList<PetDetail> queryPage(@RequestBody PetDetailQuery query) {
    return petDetailService.queryPage(query);
  }
}
